package com.hundsun.yr.universal.p2p.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.Utils.HeaderMap;
import com.hundsun.yr.universal.library.base.BaseAppManager;
import com.hundsun.yr.universal.library.base.BaseWebActivity;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils;
import com.hundsun.yr.universal.library.view.hsdialog.HSDialog;
import com.hundsun.yr.universal.library.widget.HSButton;
import com.hundsun.yr.universal.p2p.bean.AuthorizationDetailBean;
import com.hundsun.yr.universal.p2p.bean.UserInfo;
import com.hundsun.yr.universal.p2p.config.Constants;
import com.hundsun.yr.universal.p2p.config.Extras;
import com.hundsun.yr.universal.p2p.config.TaskConstants;
import com.hundsun.yr.universal.p2p.presenter.base.RequestPresenter;
import com.hundsun.yr.universal.p2p.presenter.impl.InitializePresenterImpl;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseActivity;
import com.hundsun.yr.universal.p2p.utils.BaseRequestTool2;
import com.hundsun.yr.universal.p2p.view.base.InitializeView;

import org.w3c.dom.Text;

import java.util.HashMap;

/**
 * 还款授权
 */
public class AuthorizationDetailActivity extends BaseActivity implements View.OnClickListener, InitializeView ,HSDialog.HSDialogOnItemClickListener{

    private TextView tv_begin_time = null;
    private TextView tv_authorization_amount = null;
    private TextView tv_end_time = null;
    private HSButton btn_authorization_cancel = null;
    private RequestPresenter mPresenter = null;
    private String zwgt_data;
    private int CANCEL_AUTHORIZATION_SUCCESS = -1;
    private String authorizationType = null;
    private TextView tv_tips = null;
    private HSDialog cancelAuthDialog;

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return true;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_authrization_detail;
    }

    @Override
    protected void initViewsAndEvents() {
        findView();
        mPresenter = new InitializePresenterImpl(mContext, this);
        loadingData();

        btn_authorization_cancel.setOnClickListener(this);

        toolbar_title.setText(getString(R.string.authorization_detail));

        if (authorizationType.equals(Extras.PAYMENT_AUTHORIZATION)){
            tv_tips.setText(R.string.pay_auth_tips);
        } else {
            tv_tips.setText(R.string.pre_auth_tips);
        }
    }

//    @Override
//    protected void onStart() {
//        super.onStart();
//        loadingData();
//    }

    private void loadingData() {
        if (authorizationType.equals(Extras.PAYMENT_AUTHORIZATION)) {
            zwgt_data = BaseRequestTool2.getFormatedDateString();
            //缴费授权详情页
            mPresenter.loadingData(TaskConstants.TASK_PAYMENTAUTHDETAIL, getString(R.string.root_url_test) + getString(R.string.api_user_test) + TaskConstants.TASK_PAYMENTAUTHDETAIL, Constants.EVENT_REFRESH_DATA,
                    BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_GET, getString(R.string.api_user_test) + TaskConstants.TASK_PAYMENTAUTHDETAIL, sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.
                            getShareProf(UserInfo.TOKENID), zwgt_data), false);
        } else {
            zwgt_data = BaseRequestTool2.getFormatedDateString();
            //还款授权详情页
            mPresenter.loadingData(TaskConstants.TASK_REPAYAUTHDETAIL, getString(R.string.root_url_test) + getString(R.string.api_user_test) + TaskConstants.TASK_REPAYAUTHDETAIL, Constants.EVENT_REFRESH_DATA,
                    BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_GET, getString(R.string.api_user_test) + TaskConstants.TASK_REPAYAUTHDETAIL, sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.
                            getShareProf(UserInfo.TOKENID), zwgt_data), false);
        }
    }

    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(R.id.common_toolbar).init();
    }

    public void findView() {
        tv_begin_time = findView(R.id.tv_begin_time);
        tv_authorization_amount = findView(R.id.tv_authorization_amount);
        tv_end_time = findView(R.id.tv_end_time);
        btn_authorization_cancel = findView(R.id.btn_authorization_cancel);
        tv_tips = findView(R.id.tv_tips);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        authorizationType = extras.getString("AUTHORIZATION");
    }

    @Override
    protected void onEveComming(EventCenter eventCenter) {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return true;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_authorization_cancel:

                cancelAuthDialog = new HSDialog(getString(R.string.tips), getString(R.string.authorization_tips), getString(R.string.no),
                        getString(R.string.yes), mContext, this).setCancelable(false);
                cancelAuthDialog.show();
                break;
        }
    }

    @Override
    public <T> void initializeViews(String task_name, T data) {
        switch (task_name) {
            case TaskConstants.TASK_REPAYAUTHDETAIL:
            case TaskConstants.TASK_PAYMENTAUTHDETAIL:
                if (data instanceof AuthorizationDetailBean){
                    AuthorizationDetailBean authorizationDetailBean = (AuthorizationDetailBean) data;
                    tv_begin_time.setText(authorizationDetailBean.start_date);
                    tv_authorization_amount.setText(authorizationDetailBean.max_amt);
                    tv_end_time.setText(authorizationDetailBean.end_date);
                }
                break;

            case TaskConstants.TASK_REPAYAUTHCANCEL:
                zwgt_data = BaseRequestTool2.getFormatedDateString();
                HashMap<String, String> hearder1 = BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_GET, mContext.getString(R.string.h5_user) +
                        "repayauthcancel", sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data);
                HeaderMap headerMap1 = new HeaderMap();
                headerMap1.setMap(hearder1);
                Bundle bundler1 = new Bundle();
                bundler1.putString(BaseWebActivity.BUNDLE_KEY_URL, data.toString());
                bundler1.putSerializable(BaseWebActivity.BUNDLE_KEY_HEADER, headerMap1);
                bundler1.putInt(BaseWebActivity.BUNDLE_KEY_LOADTYPE, BaseWebActivity.LoadType.DATA.getIntValue());
                readyGoThenKill(WebOpenAccountActivity.class, bundler1);
                if (BaseAppManager.getInstance().getActivity(AuthorizationDetailActivity.class) != null) {
                    BaseAppManager.getInstance().getActivity(AuthorizationDetailActivity.class).finish();
                }
                break;
            case TaskConstants.TASK_PAYMENTAUTHCANCEL:
//                showToast("取消授权");
//                CANCEL_AUTHORIZATION_SUCCESS = 0;
//                this.setResult(CANCEL_AUTHORIZATION_SUCCESS);
//                finish();
                zwgt_data = BaseRequestTool2.getFormatedDateString();
                HashMap<String, String> hearder = BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_GET, mContext.getString(R.string.h5_user) +
                        "paymentauthcancel", sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data);
                HeaderMap headerMap2 = new HeaderMap();
                headerMap2.setMap(hearder);
                Bundle bundler = new Bundle();
                bundler.putString(BaseWebActivity.BUNDLE_KEY_URL, data.toString());
                bundler.putSerializable(BaseWebActivity.BUNDLE_KEY_HEADER, headerMap2);
                bundler.putInt(BaseWebActivity.BUNDLE_KEY_LOADTYPE, BaseWebActivity.LoadType.DATA.getIntValue());
                readyGoThenKill(WebOpenAccountActivity.class, bundler);
                if (BaseAppManager.getInstance().getActivity(AuthorizationDetailActivity.class) != null) {
                    BaseAppManager.getInstance().getActivity(AuthorizationDetailActivity.class).finish();
                }
                break;
        }
    }

    @Override
    public <T> void initializeError(String task_name, T data) {
        switch (task_name) {
            case TaskConstants.TASK_REPAYAUTHDETAIL:

                break;
            case TaskConstants.TASK_REPAYAUTHCANCEL:

                break;
        }
    }

    @Override
    public void onItemClick(Object o, int position) {
        if (position != -1){
            if ((HSDialog) o == cancelAuthDialog) {
                if (authorizationType.equals(Extras.PAYMENT_AUTHORIZATION)) {
                    zwgt_data = BaseRequestTool2.getFormatedDateString();
                    //缴费授权取消
                    mPresenter.loadingData(TaskConstants.TASK_PAYMENTAUTHCANCEL, getString(R.string.root_url_test) + getString(R.string.api_user_test) + TaskConstants.TASK_PAYMENTAUTHCANCEL, Constants.EVENT_REFRESH_DATA,
                            BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_GET, getString(R.string.api_user_test) + TaskConstants.TASK_PAYMENTAUTHCANCEL, sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.
                                    getShareProf(UserInfo.TOKENID), zwgt_data), true);

                } else {
                    zwgt_data = BaseRequestTool2.getFormatedDateString();
                    //还款授权取消
                    mPresenter.loadingData(TaskConstants.TASK_REPAYAUTHCANCEL, getString(R.string.root_url_test) + getString(R.string.api_user_test) + TaskConstants.TASK_REPAYAUTHCANCEL, Constants.EVENT_REFRESH_DATA,
                            BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_GET, getString(R.string.api_user_test) + TaskConstants.TASK_REPAYAUTHCANCEL, sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.
                                    getShareProf(UserInfo.TOKENID), zwgt_data), true);
                }
            }
        }
    }

//    @Override
//    public void finish() {
//        super.finish();
//        this.setResult(CANCEL_AUTHORIZATION_SUCCESS,new Intent());
//    }
}
